package com.zxy.controller.v2;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zxy.entity.v2.Student;
import com.zxy.service.v2.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * @Description: TODO
 * @Author: zhuxianyu
 * @Date: 2021/12/18 15:09
 * @Version: 1.0
 */
@Controller
@RequestMapping("/v2")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/save/student", method = RequestMethod.POST)
    @ResponseBody
    public String save(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray jsonArray = jsonObject.getJSONArray("teacherlist");
        ArrayList<Student> studentArrayList = new ArrayList<>();
        //遍历json集合，取出数据
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject2 = (JSONObject) jsonArray.get(i);
            Student student = new Student();
            student.setUuid(jsonObject2.get("uuid").toString());
            student.setDistinct_id(jsonObject2.get("distinct_id").toString());
            student.setMobile(jsonObject2.get("mobile").toString());
            student.setEmail(jsonObject2.get("email").toString());
            student.setNick_name(jsonObject2.get("nick_name").toString());

            studentArrayList.add(student);
        }
        //dao层保存数据存入数据库
        studentService.save(studentArrayList);
        return "save";
    }

}
